//引入
let http = require("http");
let url = require("url");

//创建服务器
let server = http.createServer();

server.on("request", function (req, resp) {
    console.log("这是刚刚添加的")
    // console.log(req.url);
    // console.log(decodeURIComponent(req.url));
    // console.log(req.method);
    let urlstr = "http://localhost:8080/index.html?name=张三&age=18#haha";
    //必须是一个完整的url
    let href = new URL(urlstr);
    console.log(href);

    try{
        //会报错，因为req.url不是一完整的url地址
        console.log(new URL(req.url));
    } catch (e) {
        console.log(e);
    }
    console.log(url.parse(req.url));
    let reqUrl = url.parse(req.url, true);
    console.log(reqUrl.query);


    resp.setHeader("Content-Type", "text/html; charset=utf-8");
    resp.end("123，你好NodeJS" + reqUrl.query.name + "年龄：" + reqUrl.query.age);
});

server.listen(8080, function () {
    console.log("服务器启动成功");
});